package com.hd.appcommon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.appcommon.constant.RoleConstant;
import com.hd.appcommon.entity.OrgMemberRole;
import com.hd.appcommon.entity.Role;
import com.hd.appcommon.entity.RolePrivilege;
import com.hd.appcommon.mapper.RoleMapper;
import com.hd.appcommon.service.IOrgMemberRoleService;
import com.hd.appcommon.service.IRolePrivilegeService;
import com.hd.appcommon.service.IRoleService;
import com.hd.hdxgddcommon.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author administrator
 * @since 2020-11-30
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private IOrgMemberRoleService orgMemberRoleService;
    @Autowired
    private IRolePrivilegeService rolePrivilegeService;

    @Override
    @Cacheable(value = "RoleCache", key = "#p0")
    public List<Role> listByOrgMemberId(String teacherId) {
        return this.baseMapper.listByOrgMemberId(teacherId, RoleConstant.STATUS.ABLE);
    }

    @Override
    @CacheEvict(value = "PrivilegeCache", key = "#p0.roleId")
    public boolean save(Role role, List<String> privilegeIds) {
        boolean insert = this.baseMapper.insert(role) > 0;
        return insert ? save0(role.getRoleId(), privilegeIds) : false;
    }

    @Override
    @CacheEvict(value = "PrivilegeCache", key = "#p0.roleId")
    public boolean edit(Role role, List<String> privilegeIds) {
        boolean update = this.updateById(role);
        return update ? save0(role.getRoleId(), privilegeIds) : false;
    }

    @Override
    public Role getUnique(String roleName) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getRoleName, roleName)
                .last(" limit 1 ");
        return this.getOne(wrapper);
    }

    @Override
    public boolean editMember(String roleId, String[] memberIds) {
        Assert.hasText(roleId, "角色ID不能为空");
        this.orgMemberRoleService.removeByRoleId(roleId); // 一个人只有一个职位
        //memberIds为空则只清空集合
        if (memberIds == null || memberIds.length == 0) return true;
        List<OrgMemberRole> memberRoles = Arrays.stream(memberIds)
                .map(memberId -> {
                    if (StringUtils.isBlank(memberId)) return null;
                    OrgMemberRole memberRole = new OrgMemberRole();
                    memberRole.setRoleId(roleId).setOrgMemberId(memberId);
                    return memberRole;
                })
                //去空
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        // 更新管理部门 role表里

        return this.orgMemberRoleService.saveBatch(memberRoles);
    }

    private boolean save0(String roleId, List<String> privilegeIds) {
        Assert.hasText(roleId, "角色ID不能为空");
        LambdaQueryWrapper<RolePrivilege> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePrivilege::getRoleId, roleId);
        this.rolePrivilegeService.remove(wrapper);
        //为空则只清空权限集合
        if (privilegeIds == null || privilegeIds.isEmpty()) return true;
        List<RolePrivilege> rolePrivileges = privilegeIds.stream()
                //转换
                .map(privilegeId -> {
                    if (StringUtils.isBlank(privilegeId)) return null;
                    RolePrivilege rolePrivilege = new RolePrivilege();
                    rolePrivilege.setRoleId(roleId).setPrivilegeId(privilegeId);
                    return rolePrivilege;
                })
                //去空
                .filter(f -> f != null)
                .collect(Collectors.toList());
        return this.rolePrivilegeService.saveBatch(rolePrivileges);
    }


}
